﻿Imports System.IO
Imports System.Drawing.Imaging
Imports System.Net
Public Class Archivo
    Private Const _rutaTemp As String = "%systemroot%/temp/"
    Public Enum enmPesoTipo As Integer
        bytes = 0
    End Enum

    Public Shared Function ExtraerFileName(ByVal strPath As String) As String

        Dim strValue As String = ""

        If strPath.Trim = "" Then
            strValue = ""
            Return strValue
        End If

        strValue = Mid(strPath, 1, (strPath.LastIndexOf(".")))

        Return strValue

    End Function
    Public Shared Function FileNombre(ByVal strPathFile As String, Optional ByVal ConExtension As Boolean = True) As String

        Dim Value As String = ""

        If strPathFile.Trim = "" Then
            Return Value
        End If

        Dim objArchivo As New FileInfo(strPathFile)
        With objArchivo
            Value = .Name
        End With
        objArchivo = Nothing

        If ConExtension = False Then            
            Value = Mid(Value, 1, (Value.LastIndexOf(".")))
            'Value = Cadena.Left(Value, Value.Length - 4)
        End If

        Return Value

    End Function
    'Public Shared Function FileNombre(ByVal strPathFile As String, ByVal SinExtension As Integer) As String

    '    Dim strValue As String = ""

    '    'validar
    '    If strPathFile.Trim = "" Or strPathFile.Length < 1 Then
    '        Return strValue
    '    End If

    '    strValue = Mid(strPathFile, strPathFile.LastIndexOf("\") + 2, strPathFile.Length - (strPathFile.LastIndexOf("\") + 1))

    '    If SinExtension > 0 Then
    '        MsgBox((strValue.LastIndexOf(".")).ToString)
    '        strValue = Cadena.Left(strValue, strValue.Length - 4)
    '    End If

    '    Return strValue

    'End Function
    Public Shared Function FileExtension(ByVal strPathFile As String, Optional ByVal SinPunto As Boolean = False) As String

        Dim strValue As String = ""

        'validar
        If strPathFile.Trim = "" Or strPathFile.Length < 1 Then
            Return strValue
        End If

        Dim objArchivo As New FileInfo(strPathFile)
        With objArchivo
            strValue = .Extension
        End With

        objArchivo = Nothing

        If SinPunto = True Then
            strValue = Cadena.Right(strValue, strValue.Length - 1)
        End If

        Return strValue.Trim.ToLower

    End Function
    Public Shared Function FileExtension(ByVal strPathFile As String, ByVal Punto As Integer) As String

        Dim strValue As String = ""

        'validar
        If strPathFile.Trim = "" Or strPathFile.Length < 1 Then
            Return strValue
        End If

        strValue = Mid(strPathFile, strPathFile.LastIndexOf(".") + 2, strPathFile.Length - (strPathFile.LastIndexOf("\") + 1))

        If Punto > 0 Then
            strValue = "." & strValue
        End If

        Return strValue

    End Function
    Public Shared Function FilePeso(ByVal strPathArchivo As String, _
    Optional ByVal PesoTipo As enmPesoTipo = enmPesoTipo.bytes) As Long

        Dim lngValue As Long = 0

        Dim objArchivo As New FileInfo(strPathArchivo)
        With objArchivo
            lngValue = .Length
        End With
        objArchivo = Nothing
        Return lngValue
    End Function
    Public Shared Function FileFechaCreacion(ByVal strPathArchivo As String) As Long

        Dim lngValue As Long = 0

        Dim objArchivo As New FileInfo(strPathArchivo)
        With objArchivo
            lngValue = .CreationTime.ToFileTime
        End With
        objArchivo = Nothing
        Return lngValue

    End Function
    Public Shared Function FileFechaModificacion(ByVal strPathArchivo As String) As Long

        Dim lngValue As Long = 0

        Dim objArchivo As New FileInfo(strPathArchivo)
        With objArchivo
            lngValue = .LastWriteTime.ToFileTime
        End With
        objArchivo = Nothing
        Return lngValue

    End Function
    Public Shared Function FileExiste(ByVal PathFile As String) As Boolean

        Dim blnValue As Boolean = False

        If PathFile.Trim = "" Or PathFile.Length < 1 Then
            Return blnValue
        End If

        blnValue = System.IO.File.Exists(PathFile)

        Return blnValue
    End Function
    Public Shared Function FileDelete(ByVal PathFile As String) As Boolean

        Dim blnValue As Boolean = False

        If PathFile.Trim = "" Or PathFile.Length < 1 Then
            Return blnValue
        End If

        Try
            System.IO.File.Delete(PathFile)
            blnValue = True
        Catch ex As Exception

        End Try

        Return blnValue
    End Function
    Public Shared Function FileConvertByte(ByVal strPathArchivo As String) As Byte()

        Dim bytValue As Byte() = Nothing

        If strPathArchivo <> "" And strPathArchivo.Length > 1 Then
            Dim fs As System.IO.FileStream = New IO.FileStream(strPathArchivo, System.IO.FileMode.Open)
            bytValue = New Byte(fs.Length - 1) {}
            fs.Read(bytValue, 0, Convert.ToInt32(fs.Length))
            fs.Close()
        End If

        Return bytValue

    End Function
    Public Shared Function FileCreateByte(ByVal ArchivoByte As Byte(), ByVal PathArchivo As String, _
        ByVal NombreArchivoconExtension As String) As Boolean

        Dim blnValue As Boolean = False

        If Not ArchivoByte Is Nothing Then
            If ArchivoByte.Length > 0 Then

                Dim fs As New IO.FileStream(PathArchivo & NombreArchivoconExtension, _
                IO.FileMode.Create, IO.FileAccess.Write)
                fs.Write(ArchivoByte, 0, ArchivoByte.Length)
                fs.Close()
                blnValue = True
            End If
        End If

        Return blnValue

    End Function
    Public Shared Function FileCreateByte(ByVal ArchivoByte As Byte(), ByVal PathNombreArchivo As String) As Boolean

        Dim blnValue As Boolean = False

        If Not ArchivoByte Is Nothing Then

            If ArchivoByte.Length > 0 Then
                Dim fs As New IO.FileStream(PathNombreArchivo, IO.FileMode.Create, IO.FileAccess.Write)
                fs.Write(ArchivoByte, 0, ArchivoByte.Length)
                fs.Close()
                blnValue = True
            End If

        End If

        Return blnValue

    End Function
    Public Shared Function EjecutarApp(ByVal PathArchivo As String, ByVal Arguments As String()) As Boolean

        Dim blnValue As Boolean = False
        Dim strArgumento As String = ""
        Dim strMensaje As String = ""

        If Arguments.Length > 0 Then
            For Each str As String In Arguments
                strArgumento = strArgumento & str & " "
            Next
            'strArgumento = Components.Cadena.Left(strArgumento, strArgumento.Length - 1)
        End If

        Try
            Process.Start(PathArchivo, strArgumento)
            blnValue = True
        Catch ex As Exception
            strMensaje = "No se encontró el archivo; " _
            & Chr(13) & PathArchivo _
            & Chr(13) & "en el directorio especificado"
            MsgBox(strMensaje, 48, "EjecutarApp")
        End Try

        Return blnValue

    End Function
    Public Shared Function EjecutarApp(ByVal PathArchivo As String) As Boolean

        Dim blnValue As Boolean = False
        Dim strMensaje As String = ""

        Try
            If PathArchivo.Trim.Length > 3 Then
                Process.Start(PathArchivo)
                blnValue = True
            End If
        Catch ex As Exception
            strMensaje = "No se encontró el archivo; " _
            & Chr(13) & PathArchivo _
            & Chr(13) & "en el directorio especificado"
            MsgBox(strMensaje, 48, "EjecutarApp")
        End Try

        Return blnValue

    End Function
   
#Region "Imagen"
    Public Shared Sub SaveJPGWithCompressionSetting(ByVal image As System.Drawing.Image, _
    ByVal PathFileName As String, ByVal lCompression As Long)

        Dim eps As EncoderParameters = New EncoderParameters(1)
        eps.Param(0) = New EncoderParameter(Encoder.Quality, lCompression)
        Dim ici As ImageCodecInfo = GetEncoderInfo("image/jpeg")

        Try
            image.Save(PathFileName, ici, eps)
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical, "Components/Archivo")
        End Try

    End Sub
    Private Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo

        Dim j As Integer = 0
        Dim encoders As ImageCodecInfo()
        encoders = ImageCodecInfo.GetImageEncoders()
        For j = 0 To encoders.Length
            If encoders(j).MimeType = mimeType Then
                Return encoders(j)
            End If
        Next j
        Return Nothing

    End Function
#End Region
#Region "Directorio"
    Public Shared Function DirectorioExiste(ByVal PathDirectorio As String) As Boolean

        Dim blnValue As Boolean = False

        If PathDirectorio.Trim = "" Or PathDirectorio.Length < 1 Then
            Return blnValue
        End If

        blnValue = Directory.Exists(PathDirectorio)

        Return blnValue
    End Function
    Public Shared Function DirectorioCreate(ByVal PathDirectorio As String) As Boolean

        Dim blnValue As Boolean = False

        If PathDirectorio.Trim = "" Or PathDirectorio.Length < 1 Then
            Return blnValue
        End If

        My.Computer.FileSystem.CreateDirectory(PathDirectorio)
        blnValue = True

        Return blnValue
    End Function
    Public Shared Function DirectorioDelete(ByVal PathDirectorio As String) As Boolean

        Dim blnValue As Boolean = False

        If PathDirectorio.Trim = "" Or PathDirectorio.Length < 1 Then
            Return blnValue
        End If

        Try
            My.Computer.FileSystem.DeleteDirectory(PathDirectorio, FileIO.DeleteDirectoryOption.DeleteAllContents)
            'IO.Directory.Delete(
            blnValue = True
        Catch ex As Exception
            'Windows.Forms.MessageBox.Show(ex.Message.ToString, "", _
            'Windows.Forms.MessageBoxButtons.OK, Windows.Forms.MessageBoxIcon.Exclamation)
        End Try

        Return blnValue
    End Function
    Public Shared Function DirectorioNombre(ByVal strPathArchivo As String) As String

        Dim strValue As String = ""

        Dim objArchivo As New FileInfo(strPathArchivo)
        With objArchivo
            strValue = .DirectoryName
        End With
        objArchivo = Nothing
        Return strValue
    End Function
    Public Shared Sub DirectorioDeteleFileAll(ByVal Directorio As String, _
    Optional ByVal EliminarSubDirectorio As Boolean = False, Optional ByVal ExtensionArchivo As String = "*")

        Dim strFiles As String() = Nothing
        Dim strExtension As String = ""

        strExtension = "*." & ExtensionArchivo

        strFiles = Directory.GetFiles(Directorio, strExtension, SearchOption.AllDirectories)

        Dim strCarpeta As String = ""
        Dim strNombreArchivo As String = ""
        Dim objFile As FileInfo = Nothing

        For Each str As String In strFiles
            'objFile = New FileInfo(str)
            'strNombreArchivo = objFile.FullName          
            File.Delete(str)
        Next

        If EliminarSubDirectorio = True Then

            Dim strDir As String() = Nothing
            strDir = Directory.GetDirectories(Directorio, "*", SearchOption.AllDirectories)

            For Each str As String In strDir
                'objFile = New FileInfo(str)
                'strNombreArchivo = objFile.FullName 
                If Directory.Exists(str) = True Then
                    Directory.Delete(str, True)
                End If
            Next

        End If

    End Sub
    Public Shared Sub CopiarDirectorio(ByVal DirectorioOrigen As String, ByVal DirectorioDestino As String, _
    Optional ByVal ExtensionArchivo As String = "*")

        Dim strFiles As String() = Nothing
        Dim strExtension As String = ""
        Dim strDir As String = ""
        Dim strBackSlash As String = ""

        strExtension = "*." & ExtensionArchivo

        strDir = Vichama.Components.Cadena.Right(DirectorioOrigen, 1)

        If strDir.Contains("\") = True Then
            strBackSlash = "\"
        End If

        strFiles = Directory.GetFiles(DirectorioOrigen, strExtension, SearchOption.AllDirectories)

        Dim strCarpeta As String = ""
        Dim strNombreArchivo As String = ""
        Dim objFile As FileInfo = Nothing

        For Each str As String In strFiles

            objFile = New FileInfo(str)

            strCarpeta = objFile.DirectoryName & strBackSlash
            strCarpeta = strCarpeta.Replace(DirectorioOrigen, "")
            strNombreArchivo = str.Replace(DirectorioOrigen, "")

            If Not Directory.Exists(DirectorioDestino & strCarpeta) = True Then
                Directory.CreateDirectory(DirectorioDestino & strCarpeta)
            End If

            File.Copy(str, DirectorioDestino + strNombreArchivo, True)

        Next

    End Sub
    Public Shared Sub DirectorioCopiar(ByVal DirectorioOrigen As String, ByVal DirectorioDestino As String, _
    Optional ByVal ExtensionArchivo As String = "*", Optional ByVal CarpetaOrigen As String = "")

        Dim strFiles As String() = Nothing
        Dim strExtension As String = ""
        Dim strDir As String = ""
        Dim strBackSlash As String = ""

        strExtension = "*." & ExtensionArchivo

        strDir = Vichama.Components.Cadena.Right(DirectorioOrigen, 1)

        If strDir.Contains("\") = True Then
            strBackSlash = "\"
        End If

        strFiles = Directory.GetFiles(DirectorioOrigen & CarpetaOrigen, strExtension, SearchOption.AllDirectories)

        Dim strCarpeta As String = ""
        Dim strNombreArchivo As String = ""
        Dim objFile As FileInfo = Nothing

        For Each str As String In strFiles

            objFile = New FileInfo(str)

            strCarpeta = objFile.DirectoryName & strBackSlash
            strCarpeta = strCarpeta.Replace(DirectorioOrigen, "")
            strNombreArchivo = str.Replace(DirectorioOrigen, "")

            If Not Directory.Exists(DirectorioDestino & strCarpeta) = True Then
                Directory.CreateDirectory(DirectorioDestino & strCarpeta)
            End If

            File.Copy(str, DirectorioDestino + strNombreArchivo, True)

        Next

    End Sub
#End Region
#Region "Web"
    Public Class Web
        Public Shared Function FileURLExiste(ByVal FileURL As String, Optional ByVal MensajeShow As Boolean = False) As Boolean

            Dim blnValue As Boolean = False

            If FileURL.Trim = "" Or FileURL.Length < 4 Then
                Return blnValue
            End If

            Try

                Dim fr As System.Net.HttpWebRequest
                Dim targetURI As New Uri(FileURL)

                fr = DirectCast(System.Net.HttpWebRequest.Create(targetURI), System.Net.HttpWebRequest)
                'In the above code http://weblogs.asp.net/farazshahkhan is used as an example
                'it can be a different domain with a different filename and extension

                If (fr.GetResponse().ContentLength > 0) Then
                    'Dim str As New System.IO.StreamReader(fr.GetResponse().GetResponseStream())
                    'Response.Write(str.ReadToEnd())
                    blnValue = True
                End If

            Catch ex As System.Net.WebException
                If MensajeShow = True Then
                    MsgBox("No existe el archivo", 36, "Components/Archivo")
                End If
                Return blnValue
            End Try

            Return blnValue

        End Function
        Public Shared Function FileURLExiste(ByVal FileURL As String, ByVal ProxyServidor As String, _
        ByVal ProxyPuerto As Integer, ByVal ProxyUsuario As String, _
        ByVal ProxyClave As String, Optional ByVal MensajeShow As Boolean = False) As Boolean

            Dim blnValue As Boolean = False

            If FileURL.Trim = "" Or FileURL.Length < 4 Then
                Return blnValue
            End If

            Try

                Dim objWebProxy As Net.WebProxy = New Net.WebProxy(ProxyServidor, ProxyPuerto)
                Dim fr As System.Net.HttpWebRequest
                Dim targetURI As New Uri(FileURL)

                objWebProxy.Credentials = CredentialCache.DefaultCredentials
                objWebProxy.BypassProxyOnLocal = False
                fr = DirectCast(System.Net.HttpWebRequest.Create(targetURI), System.Net.HttpWebRequest)
                fr.Proxy = objWebProxy
                fr.Credentials = New Net.NetworkCredential(ProxyUsuario, ProxyClave)

                If (fr.GetResponse().ContentLength > 0) Then
                    'Dim str As New System.IO.StreamReader(fr.GetResponse().GetResponseStream())
                    'Response.Write(str.ReadToEnd())
                    blnValue = True
                End If

            Catch ex As System.Net.WebException
                If MensajeShow = True Then
                    MsgBox("No existe el archivo", 36, "Components/Archivo")
                End If
                Return blnValue
            End Try

            Return blnValue

        End Function
        Public Shared Function FileDownload(ByVal Url As String, ByVal PathFile As String) As Boolean

            Dim blnValue As Boolean = False
            Try
                My.Computer.Network.DownloadFile(Url, PathFile)
            Catch ex As Exception
                Return blnValue
            End Try

            blnValue = True

            Return blnValue

        End Function
        Public Shared Function FileDownload(ByVal Url As String, ByVal PathFile As String, _
        ByVal EliminarArchivoLocal As Boolean) As Boolean

            Dim blnValue As Boolean = False
            Try
                If EliminarArchivoLocal = True Then
                    If FileExiste(PathFile) = True Then
                        FileDelete(PathFile)
                    End If
                End If

                My.Computer.Network.DownloadFile(Url, PathFile)
            Catch ex As Exception
                Return blnValue
            End Try

            blnValue = True

            Return blnValue

        End Function
        Public Shared Function FileDownload(ByVal Url As String, ByVal PathFile As String, _
        ByVal ProxyServidor As String, ByVal ProxyPuerto As Integer, ByVal ProxyUsuario As String, _
        ByVal ProxyClave As String) As Boolean

            Dim blnValue As Boolean = False

            Dim objClient As New Net.WebClient
            Dim objWebProxy As Net.WebProxy = New Net.WebProxy(ProxyServidor, ProxyPuerto)

            objWebProxy.Credentials = CredentialCache.DefaultCredentials
            objClient.Proxy = objWebProxy
            objClient.Credentials = New Net.NetworkCredential(ProxyUsuario, ProxyClave)

            'If My.Computer.FileSystem.FileExists(_path + _dr("arc_dig_nom").ToString) Then
            '    My.Computer.FileSystem.DeleteFile(_path + _dr("arc_dig_nom").ToString)
            'End If

            objClient.DownloadFile(Url, PathFile)

            blnValue = True

            Return blnValue

        End Function
    End Class
#End Region


End Class
